package cybercode.carsaleserver.dto;

import lombok.Data;

import java.io.Serializable;


@Data
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private int code;

    private String msg;

    private T data;


    private R(ResultCode resultCode) {
        this(resultCode, (T) null, resultCode.getMessage());
    }

    private R(ResultCode resultCode, String msg) {
        this(resultCode, null, msg);
    }

    private R(ResultCode resultCode, T data) {
        this(resultCode, data, resultCode.getMessage());
    }

    private R(ResultCode resultCode, T data, String msg) {
        this(resultCode.getCode(), data, msg);
    }

    private R(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public boolean ok() {
        return this.code == ResultCode.SUCCESS.getCode();
    }


    public static <T> R<T> success() {
        return new R<>(ResultCode.SUCCESS);
    }

    public static <T> R<T> success(String msg) {
        return new R<>(ResultCode.SUCCESS, msg);
    }

    public static <T> R<T> fail(String msg) {

        return new R<>(ResultCode.FAILURE, msg);
    }

    public static <T> R<T> fail(ResultCode resultCode) {

        return new R<>(resultCode);
    }

    public static <T> R<T> fail(ResultCode resultCode, String msg) {

        return new R<>(resultCode, msg);
    }


    public static <T> R<T> data(T data) {
        return data(data, ResultCode.SUCCESS.getMessage());
    }

    public static <T> R<T> data(T data, String msg) {
        return data(ResultCode.SUCCESS, msg, data);
    }

    public static <T> R<T> data(ResultCode code, String msg, T data) {
        return new R<>(code, data, data == null ? "暂无承载数据" : msg);
    }

    public static <T> R<T> data(int code, T data, String msg) {
        return new R<>(code, data, data == null ? "暂无承载数据" : msg);
    }


}
